package com.zhazha.earlybird.dao;

import com.zhazha.earlybird.beans.ParttimeJobInfo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class ParttimeJobDAO {
	
	private SQLiteDatabase mDatabase;
	
	private static final String PARTTIME_JOB_TABLE = "parttime_job";
	
	private static final String PARTTIME_JOB_ID = "id"; 
	private static final String PARTTIME_JOB_TITLE = "title";
	private static final String PARTTIME_JOB_KIND_ID = "kind_id";
	private static final String PARTTIME_JOB_CITY_ID = "city_id";
	private static final String PARTTIME_JOB_HIT_COUNT = "hit_count";
	
	public ParttimeJobDAO(SQLiteDatabase datebase) {
		this.mDatabase = datebase;
	}
	
	public ParttimeJobInfo[] queryAll() {
		ParttimeJobInfo[] info = null;
		
		try {
			
			 Cursor c = this.mDatabase.query(PARTTIME_JOB_TABLE, null, null, null, null, null, "id asc");
			
			if(c.moveToFirst()) {
				info = new ParttimeJobInfo[c.getCount()];
				for (int i = 0; i < c.getCount(); i++) {
					
					info[i] = new ParttimeJobInfo();
					info[i].setId(c.getInt(c.getColumnIndex(PARTTIME_JOB_ID)));
					info[i].setTitle(c.getString(c.getColumnIndex(PARTTIME_JOB_TITLE)));
					info[i].setCityId(c.getInt(c.getColumnIndex(PARTTIME_JOB_CITY_ID)));
					info[i].setKindId(c.getInt(c.getColumnIndex(PARTTIME_JOB_KIND_ID)));
					info[i].setHitCount(c.getInt(c.getColumnIndex(PARTTIME_JOB_HIT_COUNT)));

					c.moveToNext();
				}
				return info;
				
			}
			
			return info;
		}
		catch (Exception e) {
			e.printStackTrace();
			return info;
		}
	}
	
	public void deleteAll() {
		
		try{
			this.mDatabase.delete(PARTTIME_JOB_TABLE, null, null);
		}
		catch( Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert(ParttimeJobInfo[] info) {
		
		try{
			for(int i = 0; i < info.length; i++) {
				
				ContentValues cv = new ContentValues();
				cv.put(PARTTIME_JOB_ID, info[i].getId());
				cv.put(PARTTIME_JOB_TITLE, info[i].getTitle());
				cv.put(PARTTIME_JOB_HIT_COUNT, info[i].getHitCount());
				cv.put(PARTTIME_JOB_CITY_ID, info[i].getCityId());
				cv.put(PARTTIME_JOB_KIND_ID, info[i].getKindId());

				this.mDatabase.insert(PARTTIME_JOB_TABLE, null, cv);
			}
		}
		catch( Exception e){
			e.printStackTrace();
		}
	}

}
